/**
 * @(#)ApkConfigController.java 2016年12月1日
 * Copyright(C) 2016 ZHEXIN IT CO.,LTD. All rights reserved.
 */
package org.jsbd.boss.action;

import java.util.ArrayList;
import java.util.Arrays;
import java.util.List;
import java.util.Map;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletResponse;

import org.jsbd.boss.annotation.SystemControllerLog;
import org.jsbd.boss.common.ResponseUtils;
import org.jsbd.boss.domian.ChannelApkConfigTimeModel;
import org.jsbd.boss.domian.channel.TimeModelParentTemplete;
import org.jsbd.boss.domian.channel.TimeModelTemplete;
import org.jsbd.boss.service.ITimeModelParentTempleteService;
import org.jsbd.boss.service.ITimeModelTempleteService;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.ResponseBody;

import com.alibaba.fastjson.JSONObject;

/**
 * apk配置
 * 
 * @author <a href="mailto:xupengcheng@zhexinit.com" >徐鹏程</a>
 * @version 1.0.0
 */
@Controller
@RequestMapping("/timemodel")
public class TimeModelTempleteController {
	public static final Logger LOGGER = LoggerFactory.getLogger(TimeModelTempleteController.class);

	@Resource
	private ITimeModelParentTempleteService timeTempleteParentService;

	@Resource
	private ITimeModelTempleteService timeTempleteService;

	@RequestMapping(value = "list", method = RequestMethod.GET)
	public String list() {
		return "/timemodeltemplete/list";
	}

	@RequestMapping(value = "list", params = "json")
	@ResponseBody
	public Map<String, Object> pagelist(TimeModelParentTemplete templete, HttpServletResponse response) {
		if (templete.getIsUseful() != null && templete.getIsUseful() == -1) {
			templete.setIsUseful(null);
		}
		return timeTempleteParentService.selectTempletePageListMap(templete);
	}

	@RequestMapping(value = "add", method = RequestMethod.GET)
	public String add() {
		return "/timemodeltemplete/add";
	}

	@RequestMapping(value = "effect")
	@SystemControllerLog(description = "生效")
	public void effect(TimeModelParentTemplete templete, HttpServletResponse response) {
		try {
			templete.setName(null);
			timeTempleteParentService.updateTemplete(templete);
			ResponseUtils.responseSuccess(response);
		} catch (Exception e) {
			LOGGER.error("生效出错", e);
			ResponseUtils.responseFailure(response);
		}
	}

	@RequestMapping(value = "save", method = RequestMethod.POST)
	@SystemControllerLog(description = "新增配置")
	public void saveConfig(TimeModelParentTemplete templete, HttpServletResponse response) {
		try {
			timeTempleteParentService.insertTemplete(templete);
			ResponseUtils.responseSuccess(response);
		} catch (Exception e) {
			LOGGER.error("保存出错", e);
			ResponseUtils.responseInfoExists(response);
		}
	}

	@RequestMapping(value = "{id}/update", method = RequestMethod.GET)
	public String update(@PathVariable Integer id, Model model) {
		try {
			TimeModelParentTemplete form = new TimeModelParentTemplete();
			form.setId(id);
			TimeModelParentTemplete templete = timeTempleteParentService.selectTemplete(form);
			model.addAttribute("templete", templete);
		} catch (Exception e) {
			LOGGER.error("修改出错", e);
		}
		return "timemodeltemplete/update";
	}

	@RequestMapping(value = "templeteList")
	@ResponseBody
	public String timeModelTempleteParentList(TimeModelParentTemplete templete) {
		templete.setIsUseful(1);
		return JSONObject.toJSONString(timeTempleteParentService.selectTempleteList(templete));
	}

	@RequestMapping(value = "updateTempleteList")
	@ResponseBody
	public void timeModelTempleteParentList(TimeModelTemplete[] templete, HttpServletResponse response) {
		try {
			timeTempleteService.insertTempleteList(Arrays.asList(templete));
			ResponseUtils.responseSuccess(response);
		} catch (Exception e) {
			LOGGER.error("保存出错", e);
			ResponseUtils.responseInfoExists(response);
		}
	}

	@RequestMapping(value = "timemodelList")
	@ResponseBody
	public String timeModelTempleteList(TimeModelTemplete templete) {
		List<TimeModelTemplete> list = timeTempleteService.selectTemplete(templete);
		if (list != null && list.size() > 0) {
			List<ChannelApkConfigTimeModel> configList = new ArrayList<ChannelApkConfigTimeModel>();
			for (TimeModelTemplete temp : list) {
				ChannelApkConfigTimeModel model = new ChannelApkConfigTimeModel();
				model.setCurrPercent(temp.getCurrRate());
				model.setTimePercent(temp.getTotalRate());
				model.setTime(temp.getRateTime());
				configList.add(model);
			}
			return JSONObject.toJSONString(configList);
		}
		return "[]";
	}

	@RequestMapping(value = "update", method = RequestMethod.POST)
	@SystemControllerLog(description = "更新配置")
	public void updateConfig(TimeModelParentTemplete templete, HttpServletResponse response) {
		try {
			timeTempleteParentService.updateTemplete(templete);
			ResponseUtils.responseSuccess(response);
		} catch (Exception e) {
			LOGGER.error("修改出错", e);
			ResponseUtils.responseInfoExists(response);
		}
	}
	
	@RequestMapping(value = "del")
	@SystemControllerLog(description = "删除正态模板")
	public void del(Integer id, HttpServletResponse response) {
		try {
			timeTempleteParentService.deleteTemplete(id);
			ResponseUtils.responseSuccess(response);
		} catch (Exception e) {
			LOGGER.error("删除出错", e);
			ResponseUtils.responseFailure(response);
		}
	}

	@RequestMapping(value = "{id}/updateModel", method = RequestMethod.GET)
	public String updateModel(@PathVariable Integer id, Model model) {
		TimeModelTemplete form = new TimeModelTemplete();
		form.setParentId(id);
		List<TimeModelTemplete> templeteList = timeTempleteService.selectTemplete(form);
		model.addAttribute("templeteList", JSONObject.toJSONString(templeteList));
		return "timemodeltemplete/model";
	}

	// 基本配置查询展示接口
	@RequestMapping(value = "/batchUpdate", method = RequestMethod.POST)
	@ResponseBody
	public void batchUpdate(HttpServletResponse resp, @RequestBody TimeModelTemplete[] configs) {
		List<TimeModelTemplete> list = Arrays.asList(configs);
		timeTempleteService.saveTimeModelList(list);
		ResponseUtils.responseSuccess(resp);
	}

}
